package top.whysu.rongcloud.api;

import io.rong.models.group.GroupMember;
import io.rong.models.group.GroupModel;
import io.rong.models.response.ResponseResult;
import io.rong.util.GsonUtil;
import io.rong.util.HttpUtil;
import org.springframework.util.StringUtils;

import java.net.HttpURLConnection;
import java.net.URLEncoder;

public class Bans {
    private static final String UTF8 = "UTF-8";
    private static final String PATH = "chatroom/global-gag";
    private String appKey;
    private String appSecret;
    private RongCloudSMS rongCloudSMS;

    public RongCloudSMS getRongCloudSMS() {
        return rongCloudSMS;
    }

    public void setRongCloudSMS(RongCloudSMS rongCloudSMS) {
        this.rongCloudSMS = rongCloudSMS;
    }

    public Bans(String appKey, String appSecret) {
        this.appKey = appKey;
        this.appSecret = appSecret;
    }
    public ResponseResult add(GroupModel groupModel) throws Exception {
            StringBuilder sb = new StringBuilder();
            GroupMember[] members = groupModel.getMembers();
            GroupMember[] var5 = members;
            int var6 = members.length;
            for (int var7 = 0; var7 < var6; ++var7) {
                GroupMember member = var5[var7];
                sb.append("&groupId=").append(URLEncoder.encode(member.getGroupId(), "UTF-8"));
            }
            String body = sb.toString();
            if (body.indexOf("&") == 0) {
                body = body.substring(1, body.length());
            }

            HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/group/ban/add.json", "application/x-www-form-urlencoded");
            HttpUtil.setBodyParameter(body, conn);
            return (ResponseResult) GsonUtil.fromJson(HttpUtil.returnResult(conn), ResponseResult.class);

    }

    public ResponseResult rollback(GroupModel groupModel) throws Exception {
                StringBuilder sb = new StringBuilder();
                GroupMember[] members = groupModel.getMembers();
                GroupMember[] var5 = members;
                int var6 = members.length;
                for (int var7 = 0; var7 < var6; ++var7) {
                    GroupMember member = var5[var7];
                    sb.append("&groupId=").append(URLEncoder.encode(member.getGroupId(), "UTF-8"));
                }
                String body = sb.toString();
                if (body.indexOf("&") == 0) {
                    body = body.substring(1, body.length());
                }

                HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/group/ban/rollback.json", "application/x-www-form-urlencoded");
                HttpUtil.setBodyParameter(body, conn);
                return (ResponseResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), ResponseResult.class);

    }

    public GroupsResult get(GroupModel groupModel) throws Exception {
        String body=null;
            if(!StringUtils.isEmpty(groupModel) && groupModel.getMembers()!=null) {
                StringBuilder sb = new StringBuilder();
                GroupMember[] members = groupModel.getMembers();
                GroupMember[] var5 = members;
                int var6 = members.length;
                for (int var7 = 0; var7 < var6; ++var7) {
                    GroupMember member = var5[var7];
                    sb.append("&groupId=").append(URLEncoder.encode(member.getGroupId(), "UTF-8"));
                }
                 body = sb.toString();
                if (body.indexOf("&") == 0) {
                    body = body.substring(1, body.length());
                }
            }
            HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/group/ban/query.json", "application/x-www-form-urlencoded");
           if(body!=null){
               HttpUtil.setBodyParameter(body, conn);
           }
            return (GroupsResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), GroupsResult.class);
    }

    public ResponseResult whitelist_add(GroupModel groupModel) throws Exception {
        StringBuilder sb = new StringBuilder();
        GroupMember[] members = groupModel.getMembers();
        GroupMember[] var5 = members;
        int var6 = members.length;
        for (int var7 = 0; var7 < var6; ++var7) {
            GroupMember member = var5[var7];
            sb.append("&userId=").append(URLEncoder.encode(member.getId(), "UTF-8"));
        }
        sb.append("&groupId=").append(URLEncoder.encode(groupModel.getId().toString(), "UTF-8"));
        String body = sb.toString();
        if (body.indexOf("&") == 0) {
            body = body.substring(1, body.length());
        }

        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/group/user/ban/whitelist/add.json", "application/x-www-form-urlencoded");
        HttpUtil.setBodyParameter(body, conn);
        return (ResponseResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), ResponseResult.class);

    }

    public ResponseResult whitelist_rollback(GroupModel groupModel) throws Exception {
        StringBuilder sb = new StringBuilder();
        GroupMember[] members = groupModel.getMembers();
        GroupMember[] var5 = members;
        int var6 = members.length;
        for (int var7 = 0; var7 < var6; ++var7) {
            GroupMember member = var5[var7];
            sb.append("&userId=").append(URLEncoder.encode(member.getId(), "UTF-8"));
        }
        sb.append("&groupId=").append(URLEncoder.encode(groupModel.getId().toString(), "UTF-8"));
        String body = sb.toString();
        if (body.indexOf("&") == 0) {
            body = body.substring(1, body.length());
        }

        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/group/user/ban/whitelist/rollback.json", "application/x-www-form-urlencoded");
        HttpUtil.setBodyParameter(body, conn);
        return (ResponseResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), ResponseResult.class);

    }

    public GroupBanResult whitelist_query(GroupModel groupModel) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("&groupId=").append(URLEncoder.encode(groupModel.getId().toString(), "UTF-8"));
        String body = sb.toString();
        if (body.indexOf("&") == 0) {
            body = body.substring(1, body.length());
        }
        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/group/user/ban/whitelist/query.json", "application/x-www-form-urlencoded");
        HttpUtil.setBodyParameter(body, conn);
        return (GroupBanResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), GroupBanResult.class);

    }
}
